package com.gnerv.battle.boot.module.base.business.impl;

import com.gnerv.battle.boot.module.base.model.bo.PermissionBO;
import com.gnerv.battle.boot.module.base.model.dto.PermissionRoleDTO;
import com.gnerv.battle.boot.common.base.model.RestResult;
import com.gnerv.battle.boot.module.base.business.PermissionBusiness;
import com.gnerv.battle.boot.module.base.model.dto.PermissionCreateDTO;
import com.gnerv.battle.boot.module.base.model.dto.PermissionDTO;
import com.gnerv.battle.boot.module.base.model.dto.PermissionUpdateDTO;
import com.gnerv.battle.boot.module.base.service.PermissionService;
import com.gnerv.battle.boot.common.tools.UUIDTools;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Gnerv LiGen
 */
@Slf4j
@Service
@AllArgsConstructor
public class PermissionBusinessImpl implements PermissionBusiness {

    private final PermissionService permissionService;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> create(PermissionCreateDTO permissionCreateDTO) {
        PermissionBO permissionBO = permissionCreateDTO.toPermissionBO();
        permissionBO.setUkId(UUIDTools.lower());
        permissionService.create(permissionBO);
        return RestResult.ok();
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> deleteByUkId(String ukId) {
        permissionService.deleteByUkId(ukId);
        permissionService.unbindRole(ukId);
        return RestResult.ok();
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> updateByUkId(PermissionUpdateDTO permissionUpdateDTO) {
        permissionService.updateByUkId(permissionUpdateDTO.toPermissionBO());
        return RestResult.ok();
    }

    @Override
    @Transactional(readOnly = true)
    public RestResult<PermissionDTO> selectByUkId(String ukId) {
        return RestResult.ok(permissionService.selectByUkId(ukId));
    }

    @Override
    @Transactional(readOnly = true)
    public RestResult<List<PermissionDTO>> listByName(String name, Integer page, Integer size) {
        int total = permissionService.countByName(name);
        List<PermissionDTO> permissionDTOList = permissionService.listByName(name, page, size);
        return RestResult.ok(permissionDTOList).page(page, size, total);
    }

    @Override
    @Transactional(readOnly = true)
    public RestResult<List<PermissionDTO>> listByRoleUkId(String roleUkId) {
        return RestResult.ok(permissionService.listByRoleUkId(roleUkId));
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> bindRole(PermissionRoleDTO permissionRoleDTO) {
        permissionService.unbindRole(permissionRoleDTO.getPermissionUkId());
        permissionService.bindRole(permissionRoleDTO.getPermissionUkId(), permissionRoleDTO.getRoleUkIdList());
        return RestResult.ok();
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> unbindRole(String ukId) {
        permissionService.unbindRole(ukId);
        return RestResult.ok();
    }

}
